package MySingleLinkedList;

public class MySingleLinkedList implements IList{
    // 1、无头单向非循环链表实现
    public int usidsize;
    public ListNode head;
    public static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }


    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(this.head == null) {
            this.head = node;
        }else {
            node.next = this.head;
            this.head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(this.head == null) {
            this.head = node;
        }else {
            ListNode cur = this.head;
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    @Override
    public void addIndex(int index, int data) throws ArrayListException{
        ListNode node = new ListNode(data);
        //
        if(index < 0 || index > size()) {
            throw new ArrayListException("链表随机插入异常");
        }
        //
        if(index == 0) {
            addFirst(data);
            return;
        }
        //
        if(index == size()) {
            addLast(data);
            return;
        }
        //
        ListNode cur =  searchPrev(index);
        node.next = cur.next;
        cur.next = node;
    }
    private ListNode searchPrev(int index) {
        ListNode cur = this.head;
        int count = 0;
        while(count < index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur.next != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        ListNode cur = findPrev(key);
        if(cur == null) {
            System.out.println("没有要删除的数字");
            return;
        }
        ListNode node = cur.next;
        cur.next = node.next;
    }
    private ListNode findPrev(int key) {
        ListNode cur = this.head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }

        return null;
    }

    //方法一
//    @Override
//    public void removeAllKey(int key) {
//        ListNode cur = this.head;
//        while(cur.next != null) {
//            if(cur.next.val == key) {
//                ListNode node = cur.next;
//                cur.next = node.next;
//                continue;
//            }
//            cur = cur.next;
//        }
//        if(this.head.val == key) {
//            this.head = this.head.next;
//        }
//    }

    //方法二
    @Override
    public void removeAllKey(int key) {
        if(this.head == null) {
            return;
        }
        ListNode prve = this.head;
        ListNode cur = this.head.next;
        while(cur != null) {
            if(cur.val == key) {
                prve.next = cur.next;
                cur = cur.next;
            }else {
                cur = cur.next;
                prve = prve.next;
            }
        }

        if(this.head.val == key) {
            this.head = this.head.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = this.head;
        while(cur != null) {
            ListNode curNext = cur.next;
            //如果val是引用类型
            //cur.val = null;
            cur = null;
            cur = curNext;
        }
        this.head = null;
    }

    /**
     * 从指定位置开始打印
     */
    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void display(ListNode newNode) {
        ListNode cur = newNode;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //练习 逆置链表 oj题目
        public ListNode reverseList() {
            if(head == null) {
                return head;
            }
            if(head.next == null) {
                return head;
            }
            ListNode cur = head.next;
            head.next = null;
            while(cur != null) {
                ListNode curNext = cur.next;
                cur.next = head;
                head = cur;
                cur = curNext;
            }
            return head;
        }

    }
}
